package com.hbplat.test.jdbc.provider.service.base;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hbplat.shardingsphere.common.ServerResponse;
import com.hbplat.shardingsphere.constant.Const;
import com.hbplat.shardingsphere.util.StringUtil;
import com.hbplat.test.jdbc.provider.base.BaseService;
import com.hbplat.test.jdbc.provider.entity.base.BaseUser;
import com.hbplat.test.jdbc.provider.entity.base.dto.BaseUserDTO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 　　* @description: TODO
 * 　　* @param
 * 　　* @return
 * 　　* @throws
 * 　　* @author 陈宇
 * 　　* @date $ $
 *
 */

@Service(version = "1.0.0",interfaceClass = IBaseUserService.class,timeout = 999999999,group = "bus")
@Component
public class BaseUserServiceImpl extends BaseService implements IBaseUserService{


    /**
     * 新增/更新用户信息
     *
     * @param baseUserDTO
     * @return
     */
    @Override
    public ServerResponse<String> addBaseUser(BaseUserDTO baseUserDTO) {
        if(null == baseUserDTO || StringUtil.isEmpty(baseUserDTO.getUserStr())){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        List<Map> list = JSONObject.parseArray(baseUserDTO.getUserStr(),Map.class);
        if(null != list && 0 < list.size()){
            for(Map map : list){
                String userName = StringUtil.valueOfString(map.get("userName"));
                BaseUser baseUser = baseUserWorkMapper.selectBaseUserByUserName(userName);
                if(null == baseUser){
                    baseUser = new BaseUser();
                    baseUser.setUserName(userName);
                }
                baseUser.setDisplayName(StringUtil.valueOfString(map.get("displayName")));
                baseUser.setPassword(StringUtil.valueOfString(map.get("password")));
                baseUser.setEmail(StringUtil.valueOfString(map.get("email")));
                baseUser.setPhone(StringUtil.valueOfString(map.get("phone")));
                if(null == baseUser.getId()){
                    baseUser.setId(iSequenceService.selectSeq("base_user"));
                    baseUserMapper.insertSelective(baseUser);
                }else{
                    baseUserMapper.updateByPrimaryKeySelective(baseUser);
                }
            }
        }
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    /**
     * 批量查询用户
     * @param baseUserDTO
     * @return
     */
    @Override
    public ServerResponse<PageInfo> selectListBaseUser(BaseUserDTO baseUserDTO) {
        if(null == baseUserDTO){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        if(null == baseUserDTO.getPageNum()){
            baseUserDTO.setPageNum(Const.INITPAGENUM);
        }
        if(null == baseUserDTO.getPageSize()){
            baseUserDTO.setPageSize(Const.INITPAGESIZE);
        }
        PageHelper.startPage(baseUserDTO.getPageNum(),baseUserDTO.getPageSize(),true,false,null);
        List<Map> list = baseUserWorkMapper.selectListBaseUser(baseUserDTO);
        if(null == list || 0 == list.size()){
            return ServerResponse.createByErrorMessage("没有数据");
        }
        return ServerResponse.createBySuccess("查询成功",new PageInfo(list));
    }
}
